<template>
  <div class="update_management_box">
    <el-form label-position="top" :model="serviceObj" class="demo-form-stacked" ref="serviceObj" :rules="rules" v-if="reload">
      <el-row :gutter="20" class="mar-t-10">
        <el-col :span="9">
          <el-form-item prop="name" :label="$t('channelsettings.schedule11')">
            <el-input v-model.trim="serviceObj.name" :placeholder="$t('channelsettings.schedule12')" style="width: 100%"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item prop="order" :label="$t('channelsettings.process42')">
            <el-input v-model="serviceObj.order" :placeholder="$t('channelsettings.schedule13')" style="width: 100%"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" class="mar-t-10">
        <el-col :span="9">
          <el-form-item prop="processId" :label="$t('configLazyLoad.channel.chooseFlow')">
            <el-select class="select_process" v-model="serviceObj.processId">
            <el-option
              v-for="option in options"
              :label="option.label"
              :value="option.value">
            </el-option>
          </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" class="mar-t-10">
        <el-form-item :label="$t('report.imReportDate')" style="padding-left: 10px">
          <el-col :span="9" style="padding-left: 0px">
            <el-form-item prop="startDate">
              <el-input v-model.trim="serviceObj.startDate" placeholder="" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="bet" :span="2" style="padding-left: 6px">
          </el-col>
          <el-col :span="9">
            <el-form-item prop="endDate">
              <el-input v-model.trim="serviceObj.endDate" placeholder="" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-row>
      <el-row :gutter="20" class="mar-t-10">
        <el-form-item :label="$t('public.time')" style="padding-left: 10px">
          <el-col :span="9" style="padding-left: 0px">
            <el-form-item prop="startTime">
              <el-input v-model.trim="serviceObj.startTime" placeholder="" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="bet" :span="2" style="padding-left: 6px">
          </el-col>
          <el-col :span="9">
            <el-form-item prop="endTime">
              <el-input v-model.trim="serviceObj.endTime" placeholder="" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-row>
      <!--<el-row :gutter="20" class="mar-t-10">-->
        <!--<el-form-item label="欢迎语" style="padding-left: 10px">-->
          <!--<el-input-->
            <!--type="textarea"-->
            <!--:rows="2"-->
            <!--placeholder="请输入内容"-->
            <!--v-model.trim="serviceObj.welcomeMessage">-->
          <!--</el-input>-->
        <!--</el-form-item>-->
      <!--</el-row>-->
    </el-form>
    <div class="schedule-week ismemory mar-t-10">
      <div class="skill-name pb20">
        <p style="margin-bottom:5px;">{{$t('channelsettings.schedule14')}}</p>
        <el-checkbox v-model="serviceObj.weekMon">{{$t('channelsettings.schedule15')}}</el-checkbox>
        <el-checkbox v-model="serviceObj.weekTue">{{$t('channelsettings.schedule16')}}</el-checkbox>
        <el-checkbox v-model="serviceObj.weekWed">{{$t('channelsettings.schedule17')}}</el-checkbox>
        <el-checkbox v-model="serviceObj.weekThu">{{$t('channelsettings.schedule18')}}</el-checkbox>
        <el-checkbox v-model="serviceObj.weekFri">{{$t('channelsettings.schedule19')}}</el-checkbox>
        <el-checkbox v-model="serviceObj.weekSat">{{$t('channelsettings.schedule20')}}</el-checkbox>
        <el-checkbox v-model="serviceObj.weekSun">{{$t('channelsettings.schedule21')}}</el-checkbox>
      </div>
    </div>
    <!--<el-form :model="serviceObj" ref="ruleForm" label-width="100px" class="skill_group_node">-->
      <!--<chooseSkillCroup :peerLength="peerLength" :toPeerInfo="serviceObj.toPeer"></chooseSkillCroup>-->
      <!--<el-form-item label="启用智能机器人:">-->
        <!--<el-radio-group v-model="serviceObj.robot">-->
          <!--<el-radio :label="''">不启用</el-radio>-->
          <!--<el-radio :label="robot._id" v-for="robot in robotConfig">{{robot.robot_type}}</el-radio>-->
        <!--</el-radio-group>-->
      <!--</el-form-item>-->
    <!--</el-form>-->
    <div class="save_btn" style="clear: both">
      <el-button type="primary" @click="submit(serviceObj)" class='primary_btn'>{{$t('public.save')}}</el-button>
    </div>
  </div>
</template>
<script>
  import {checkform} from '@/utils/validate'
  import {deepClone} from '@/utils/m7Utils'
  import chooseSkillCroup from './chooseSkillGroup'

  export default {
    name: 'updateManagement',
    data () {
      var validateOptName = (rule, value, callback) => {
        let message = checkform(value, 'Number4')
        if (value === '') {
          callback(new Error(this.$t('validate.required')))
        } else if (message || value < 1) {
          callback(new Error(this.$t('channelsettings.schedule28')))
        } else {
          callback()
        }
      }
      let datePattern = new RegExp(/^([1-9]|[1][012])-([1-9]|[12][0-9]|[3][01])$/)
      var validateStartDate = (rule, value, callback) => {
        if (value === '') {
          callback(new Error(this.$t('validate.required')))
        } else if (!datePattern.test(value) && value !== '*') {
          callback(new Error(this.$t('channelsettings.schedule289')))
        } else {
          callback()
        }
      }
      var validateEndDate = (rule, value, callback) => {
        if (value === '') {
          callback(new Error(this.$t('validate.required')))
        } else if (!datePattern.test(value) && value !== '*') {
          callback(new Error(this.$t('channelsettings.schedule30')))
        } else {
          callback()
        }
      }
      let timePattern = new RegExp(/^([01][0-9]|[2][0-3]):[0-5][0-9]$/)
      var validateStartTime = (rule, value, callback) => {
        if (value === '') {
          callback(new Error(this.$t('validate.required')))
        } else if (!timePattern.test(value) && value !== '*') {
          callback(new Error(this.$t('channelsettings.schedule31')))
        } else {
          callback()
        }
      }
      var validateEndTime = (rule, value, callback) => {
        if (value === '') {
          callback(new Error(this.$t('validate.required')))
        } else if (!timePattern.test(value) && value !== '*') {
          callback(new Error(this.$t('channelsettings.schedule32')))
        } else if (value !== '*') {
          let lastNum = value.substring(value.length - 1)
          if (lastNum % 2 === 0) {
            callback(new Error(this.$t('channelsettings.schedule33')))
          } else {
            callback()
          }
        } else {
          callback()
        }
      }
      return {
        rules: {
          name: [
            {required: true, message: this.$t('validate.required'), trigger: 'blur'}
          ],
          processId: [
            {required: true, message: this.$t('configLazyLoad.channel.required'), trigger: 'change'}
          ],
          order: [
            {required: true, validator: validateOptName, trigger: 'blur'}
          ],
          startDate: [
            {required: true, validator: validateStartDate, trigger: 'blur'}
          ],
          endDate: [
            {required: true, validator: validateEndDate, trigger: 'blur'}
          ],
          startTime: [
            {required: true, validator: validateStartTime, trigger: 'blur'}
          ],
          endTime: [
            {required: true, validator: validateEndTime, trigger: 'blur'}
          ]
        },
        reload: false,
        serviceObj: {},
        toPeerInfo: [],
        robotConfig: [],
        peerLength: 0
      }
    },
    methods: {
//      changeData (data) {
//        this.serviceObj = {
//          account: data.account,
//          name: data.name || '',
//          accessId: data.accessId,
//          accessType: data.accessType,
//          order: data.order || '',
//          startDate: data.startDate || '*',
//          endDate: data.endDate || '*',
//          startTime: data.startTime || '*',
//          endTime: data.endTime || '*',
//          weekMon: data.weekMon || true,
//          weekTue: data.weekTue || true,
//          weekWed: data.weekWed || true,
//          weekThu: data.weekThu || true,
//          weekFri: data.weekFri || true,
//          weekSat: data.weekSat || true,
//          weekSun: data.weekSun || true,
//          processId: data.processId || '',
//          createTime: data.createTime || '',
//          createUser: data.createUser || '',
//          updateTime: data.updateTime || '',
//          updateUser: data.updateUser || ''
//        }
//      },
      submit (data) {
        let that = this
        that.$refs.serviceObj.validate((valid) => {
          if (valid) {
//            if (data.toPeer.length === 0) {
//              that.$message.error('请选择技能组')
//              return
//            }
//            that.robotConfig.map((item) => {
//              if (data.robot === item._id) {
//                if (item.robot_type === '小七') {
//                  data.robotType = '7mbot'
//                } else {
//                  data.robotType = 'sobot'
//                }
//              }
//            })

            let action = 'addSchedule'
            if (that.type === 'edit') {
              action = 'webchatUpdateSchedule'
            }
            data.order = parseInt(data.order)
            this.$store.dispatch(action, data).then((res) => {
              if (res && res.success) {
                that.back(data.accessId)
              } else {
                that.$message({
                  message: res.message,
                  type: 'error'
                })
              }
            })
          }
        })
      },
      back (data) {
        this.$emit('back', data)
      }
    },
    props: {
      updateInfo: {
        type: Object,
        default: function () {
          return {}
        }
      },
      randomNum: {
        type: Number,
        default: 0
      },
      type: {
        type: String,
        default: 'add'
      },
      manageType: {
        type: String,
        default: 'web'
      }
    },
    components: {
      chooseSkillCroup
    },
    computed: {
      account () {
        return this.$store.state.session.account.account
      },
      options () {
        let allProcess = this.$store.state.config.channelConfig.webchatSchdule.webchatProcessLists
        let arr = []
        let type = 'common'
        if (this.manageType === 'wechat') {
          type = 'wechat'
        } else if (this.manageType === 'weibo') {
          type = 'weibo'
        } else if (this.manageType === 'sdk') {
          type = 'sdk'
        } else if (this.manageType === 'wechatMini') {
          type = 'wxmini'
        }
        let flag = true
        allProcess.forEach((item) => {
          if (item.firstNodeAccessType === type) {
            arr.push(item)
          }
          if (item._id === this.serviceObj.processId) {
            flag = false
          }
        })
        if (flag) {
          this.serviceObj.processId = ''
        }
        let arrNew = arr.map(item => ({label: item.name, value: item._id}))
        return arrNew
      }
    },
    watch: {
      randomNum () {
        this.reload = false
        this.$nextTick(() => {
          this.reload = true
          if (this.type === 'edit') {
            this.serviceObj = deepClone(this.updateInfo)
          } else {
            this.serviceObj = {
              account: this.updateInfo.account,
              name: '',
              accessId: this.updateInfo.accessId,
              accessType: this.updateInfo.accessType,
              order: '',
              startDate: '*',
              endDate: '*',
              startTime: '*',
              endTime: '*',
              weekMon: true,
              weekTue: true,
              weekWed: true,
              weekThu: true,
              weekFri: true,
              weekSat: true,
              weekSun: true,
              processId: '',
              createTime: '',
              createUser: '',
              updateTime: '',
              updateUser: ''
            }
          }
        })
      }
    },
    beforeMount () {
      this.reload = true
      this.serviceObj = deepClone(this.updateInfo)
//      if (this.manageType === 'wechat' || this.manageType === 'weibo') {
//        this.peerLength = 1
//      }
      this.$store.dispatch('getCache', {type: 'robotConfig'}).then((res) => {
        if (res) {
          this.robotConfig = deepClone(res)
        }
      })
    }
  }
</script>
<style lang="stylus" scoped>
  .save_btn
    margin-top 20px
  .select_process
    width 100%
</style>
